package cn.iocoder.yudao.module.crossborder.order.controller.admin.orderaddress;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.order.controller.admin.orderaddress.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.orderaddress.OrderAddressDO;
import cn.iocoder.yudao.module.crossborder.order.service.orderaddress.OrderAddressService;

@Tag(name = "管理后台 - 跨境电商订单地址")
@RestController
@RequestMapping("/crossborder/order-address")
@Validated
public class OrderAddressController {

    @Resource
    private OrderAddressService orderAddressService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商订单地址")
    @PreAuthorize("@ss.hasPermission('crossborder:order-address:create')")
    public CommonResult<Long> createOrderAddress(@Valid @RequestBody OrderAddressSaveReqVO createReqVO) {
        return success(orderAddressService.createOrderAddress(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商订单地址")
    @PreAuthorize("@ss.hasPermission('crossborder:order-address:update')")
    public CommonResult<Boolean> updateOrderAddress(@Valid @RequestBody OrderAddressSaveReqVO updateReqVO) {
        orderAddressService.updateOrderAddress(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商订单地址")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:order-address:delete')")
    public CommonResult<Boolean> deleteOrderAddress(@RequestParam("id") Long id) {
        orderAddressService.deleteOrderAddress(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商订单地址")
                @PreAuthorize("@ss.hasPermission('crossborder:order-address:delete')")
    public CommonResult<Boolean> deleteOrderAddressList(@RequestParam("ids") List<Long> ids) {
        orderAddressService.deleteOrderAddressListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商订单地址")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order-address:query')")
    public CommonResult<OrderAddressRespVO> getOrderAddress(@RequestParam("id") Long id) {
        OrderAddressDO orderAddress = orderAddressService.getOrderAddress(id);
        return success(BeanUtils.toBean(orderAddress, OrderAddressRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商订单地址分页")
    @PreAuthorize("@ss.hasPermission('crossborder:order-address:query')")
    public CommonResult<PageResult<OrderAddressRespVO>> getOrderAddressPage(@Valid OrderAddressPageReqVO pageReqVO) {
        PageResult<OrderAddressDO> pageResult = orderAddressService.getOrderAddressPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderAddressRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商订单地址 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:order-address:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderAddressExcel(@Valid OrderAddressPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderAddressDO> list = orderAddressService.getOrderAddressPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商订单地址.xls", "数据", OrderAddressRespVO.class,
                        BeanUtils.toBean(list, OrderAddressRespVO.class));
    }

}